<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Comment extends Model
{
    //
    public function add()
    {
        if (!user_ins()->is_logged_in()) {
            return ['status' => 0, 'msg' => 'login required'];
        }
        // 传参中是否有内容
        if (!rq('content')) {
            return ['status' => 0, 'msg' => 'empty content'];
        }

        // 检查是否存在 question_id 或 answer_id
        if  (
        (!rq('question_id') && !rq('answer_id')) ||  // none
        (rq('question_id') && rq('answer_id')) ){    // all
            return ['status' => 0, 'msg' => 'question_id or answer_id is required'];
        }
        // 如果有问题id
        if (rq('question_id')) {
            // 查找出评论问题
            $question = question_ins()->find(rq('question_id'));
            if (!$question) return ['status' => 0, 'msg' => 'question not exists'];
            $this->question_id = rq('question_id');
        } else {
            // 评论答案
            $answer = answer_ins()->find(rq('answer_id'));
            // 检查答案是否存在
            if (!$answer) return ['status' => 0, 'msg' => 'answer not exists'];
            $this->answer_id = rq('answer_id');
        }
        // 检查目标评论是否存在
        if (rq('reply_to')) {
            $target = $this->find(rq('reply_to'));
            // 检查目标评论是否存在
            if (!$target) return ['status' => 0, 'msg' => 'target comment not exists'];
            // 检查是否在回复自己的评论
            if ($target->user_id == session('user_id')) {
                return ['status' => 0, 'msg' => 'cannot replay to yourself'];
            }
            $this->reply_to = rq('reply_to');
        }

        $this->content = rq('content');
        $this->user_id = session('user_id');

        return $this->save() ?
            ['status' => 1, 'id' => $this->id] :
            ['status' => 0, 'msg' => 'db insert failed'];
    }

    // 查看评论 api
    public function read()
    {
        // 如果问题 id 和 回答 id都不存在
        if (!rq('question_id') && !rq('answer_id')) {
            return ['status' => 0, 'question_id or answer_id is required'];
        }
        // 如果存在 question_id
        if (rq('question_id')) {
            $question = question_ins()->find(rq('question_id'));
            if (!$question) return ['status' => 0, 'question not exists'];
            $data = $this->where('question_id', rq('question_id'));
        } else {
            $answer = answer_ins()->find(rq('answer_id'));
            if (!$answer) return ['status' => 0, 'answer not exists'];
            $data = $this->where('answer_id', rq('answer_id'));
        }

        $data = $data->get()->keyBy('id');
        return ['status' => 1, 'data' => $data];
    }

    // 删除评论 api
    public function remove()
    {
        // 检查是否登录
        if (!user_ins()->is_logged_in()) {
            return ['status' => 0, 'msg' => 'login required'];
        }

        // 检查传参中是否存在 id
        if (!rq('id')) {
            return ['status'=> 0, 'msg' => 'id is required'];
        }

        // 查找当前id的评论
        $comment = $this->find(rq('id'));
        if (!$comment) return ['status' => 0, 'msg' => 'comment not exists'];

        // 如果评论中的用户不是当前登录的用户
        if ($comment->user_id != session('user_id')) {
            return ['status' => 0, 'msg' => 'permission denied'];
        }
        // 先删除此评论下的所有回复
        $this->where('reply_to', rq('id'))->delete();

        return $comment->delete() ?
            ['status' => 1] :
            ['status' => 0, 'db delete failed'];
    }
}
